from datetime import datetime
import os
import logging
from logging import handlers

class LogLevel:
    DEBUG = "debug"
    INFO = "info"
    WARNINGS = "warning"
    ERROR = "error"
    FATAL = "fatal"


class Log:
    @staticmethod
    def init_log():
        when = "D"
        log_dir = "log"
        file_name = "normal.log"
        error_file_name = "error.log"
        file_name = os.path.join(log_dir, file_name)
        error_file_name = os.path.join(log_dir,error_file_name)
        LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
        formatter = logging.Formatter(LOG_FORMAT)

        if not os.path.exists(log_dir):
            os.mkdir(log_dir)

        #logging.basicConfig(filename=file_name, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)

        logger = logging.getLogger()
        logger.setLevel(logging.INFO)

        for handler in logger.handlers:
            logger.removeHandler(handler)

        # 正常log
        normal_file_handler = handlers.TimedRotatingFileHandler(file_name,when,1,backupCount=20)
        normal_file_handler.setLevel(logging.INFO)
        normal_file_handler.setFormatter(formatter)
        normal_file_handler.suffix = "%Y%m%d.log"
        logger.addHandler(normal_file_handler)
        
        # 报错log
        error_file_handler = handlers.TimedRotatingFileHandler(error_file_name,when,1,backupCount=20)
        error_file_handler.setLevel(logging.ERROR)
        error_file_handler.setFormatter(formatter)
        error_file_handler.suffix = "%Y%m%d.log"
        logger.addHandler(error_file_handler)


    @staticmethod
    def log_msg(message):
        time_stamp = datetime.now()
        time_str = time_stamp.strftime('%Y.%m.%d-%H:%M:%S.%f')
        print('[{0}]:{1}'.format(time_str,message))

    @staticmethod
    def debug(message):
        logging.debug(message)

    @staticmethod
    def info(message):
        logging.info(message)

    @staticmethod
    def warning(message):
        logging.warning(message)

    @staticmethod
    def error(message):
        logging.error(message)

    @staticmethod
    def critical(message):
        logging.critical(message)

    

    @staticmethod
    def debug_list(data_list):
        if data_list and len(data_list):
            for obj in data_list:
                obj.print_object()

